
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.sql.rowset.WebRowSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class LoginForm extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField txtuser;
	private JPasswordField txtpass;
	
	JButton btnLogin;
	
	WebRowSet rs=null;
	DataProcess data=null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginForm frame = new LoginForm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginForm() {
		setResizable(false);
		setTitle("Login");
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 281, 163);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsername = new JLabel("UserName");
		lblUsername.setBounds(10, 24, 72, 21);
		contentPane.add(lblUsername);
		
		txtuser = new JTextField();
		txtuser.setBounds(94, 24, 161, 20);
		contentPane.add(txtuser);
		txtuser.setColumns(10);
		
		JLabel lblPassword = new JLabel("PassWord");
		lblPassword.setBounds(10, 56, 72, 21);
		contentPane.add(lblPassword);
		
		btnLogin = new JButton("Login");
		btnLogin.addActionListener(this);
		btnLogin.setBounds(43, 93, 89, 23);
		contentPane.add(btnLogin);
		
		JButton btnUndo = new JButton("Undo");
		btnUndo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtuser.setText("");
				txtpass.setText("");
				txtuser.requestFocus();
			}
		});
		btnUndo.setBounds(142, 93, 89, 23);
		contentPane.add(btnUndo);
		
		txtpass = new JPasswordField();
		txtpass.setBounds(94, 55, 161, 20);
		contentPane.add(txtpass);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnLogin){
			if(txtuser.getText().toString().length()==0){
				JOptionPane.showMessageDialog(null, "Ban chua nhap user?");
				txtuser.requestFocus();
			}else if(txtpass.getText().toString().length()==0){
				JOptionPane.showMessageDialog(null, "Ban chua nhap password?");
				txtpass.requestFocus();
			}else{

				String user=txtuser.getText();
				String pass= txtpass.getText();
				try {
					String sql="Select UserName,PassWord,GrantUser from Login where UserName='" + user + "' and PassWord='" + pass + "'";
					rs=data.GetDataBySQL(sql);
					while(rs.next()){
						if((rs.getString(1).equals(user)) && (rs.getString(2).equals(pass)) && (rs.getInt(3)==1)){
							if(user.equals("admin")){
								AdminApp frm = new AdminApp(user);
								frm.show();
								setVisible(false);
							}
							else{
								Register frm = new Register(user);
								frm.show();
								setVisible(false);
							}
						}
						else{
							JOptionPane.showMessageDialog(null,"You are not allow to login!");
						}
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
		}
		
	}
}
